//
//  PeiTaoViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/2.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "PeiTaoViewController.h"

#import "PeiTaoCell.h"
#import "TodoHttpManager.h"
#import "PrefixHeader.pch"
#import "PeiTaoXVC.h"
#import "FTPopOverMenu.h"
@interface PeiTaoViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

{
    NSMutableArray *_busDataArr;
    NSMutableDictionary *_peiGDic;
    
    NSString *uid;
    
    //全局view,好对其删除.
    UIView*view;
    //建两个全局的,来记录你点击了什么
    NSString *one;
    NSString *tow;
    
    NSString *IDA;
    NSString *keStr;
}
@property(nonatomic,strong)UITableView *tablView;
@property(nonatomic,strong)UISearchBar *souSearchBar;

@end

@implementation PeiTaoViewController



-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didDeatails:) name:TDPeiRenGou object:nil];

    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    uid= [userDefa objectForKey:@"USER_ID"];
    [_manager userGMPeiRenGouWithID:uid withSouSuo:nil withSouType:nil withWuYe:nil withFangK:nil];
   
}

-(void)didDeatails:(NSNotification *)notificat
{
    if (notificat.object != nil)
    {
        int status = [[notificat.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            _peiGDic = [notificat.object objectForKey:@"data"];
            _busDataArr = [_peiGDic objectForKey:@"data"];
            NSString *taoStr = [_peiGDic objectForKey:@"cw"];
            NSString *jinStr = [_peiGDic objectForKey:@"ccs"];
            [self setupViewWithTaoSh:taoStr withMoney:jinStr];
            [_tablView reloadData];
        }
        else
        {
            [_manager MakeMyselfAlert:@"获取失败" andIsNeedTitle:NO andShowTime:2.0];
        }
    }

    
    
    
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDPeiRenGou object:nil];
 
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BgColor;
    self.title= @"认购";
    _busDataArr = [NSMutableArray arrayWithCapacity:0];
    _peiGDic = [[NSMutableDictionary alloc] init];
    _manager = [TodoHttpManager sharedManager];
 
    IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
    
    
    if ([IDA isEqualToString:@"1"])
    {
        _souSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight+30, B_WIDTH, 44)];
        _souSearchBar.delegate = self;
        [_souSearchBar setPlaceholder:@"输入搜索内容"];
        [self.view addSubview:_souSearchBar];
        
    }
    else if ([IDA isEqualToString:@"2"])
    {
        UIButton *zuoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        zuoBtn.frame = CGRectMake(0, LL_StatusBarAndNavigationBarHeight+30, 50, 44);
        zuoBtn.backgroundColor = [UIColor colorWithHexString:@"#c9c9ca"];
        zuoBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [zuoBtn setTitle:@"  客户" forState:0];
        keStr = @"0";
        [zuoBtn setTitleColor:[UIColor blackColor] forState:0];
        [zuoBtn addTarget:self action:@selector(peiGouXuanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [zuoBtn setImage:[UIImage imageNamed:@"Btn_SanJiao"] forState:0];
        [zuoBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:3];
        [self.view addSubview:zuoBtn];
        _souSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(50, LL_StatusBarAndNavigationBarHeight+30, B_WIDTH-50, 44)];
        _souSearchBar.delegate = self;
        [_souSearchBar setPlaceholder:@"输入搜索内容"];
        [self.view addSubview:_souSearchBar];
    }
    

    
  
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(busRenGouBtnClick)];
    
    self.navigationItem.rightBarButtonItem = rightBarItem;
    [self.navigationItem.rightBarButtonItem setImage:[[UIImage imageNamed:@"Btn_seacher"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    _tablView = [[UITableView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight+30+44, B_WIDTH, B_HEIGHT-(LL_StatusBarAndNavigationBarHeight+30+44)) style:UITableViewStylePlain];
    _tablView.delegate = self;
    _tablView.dataSource = self;
    _tablView.rowHeight = 60;
    _tablView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tablView];

    _tablView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(peiTaoRefresh)];
}

-(void)peiTaoRefresh
{
       [_manager userGMPeiRenGouWithID:uid withSouSuo:nil withSouType:nil withWuYe:nil withFangK:nil];
    [_tablView.mj_header endRefreshing];
    
}
-(void)setupViewWithTaoSh:(NSString *)_tao withMoney:(NSString *)_money
{
    UIView *aVi = [[UIView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, B_WIDTH, 30)];
    aVi.backgroundColor = [UIColor colorWithHexString:@"#eb6200"];
    [self.view addSubview:aVi];
    UILabel *oneLab = [[UILabel alloc] init];
    oneLab.textColor = [UIColor whiteColor];
    oneLab.font = [UIFont systemFontOfSize:14];
    oneLab.text = [NSString stringWithFormat:@"车位: %@个",_tao];
    [aVi addSubview:oneLab];
    [oneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(70, 20));
        make.left.equalTo(aVi.mas_left).offset(20);
        make.top.equalTo(aVi.mas_top).offset(5);
        
    }];
    
    
    UILabel *twLab = [[UILabel alloc] init];
    twLab.textColor = [UIColor whiteColor];
    twLab.font = [UIFont systemFontOfSize:14];
    twLab.text = [NSString stringWithFormat:@"|   储藏室:%@ 个 ",_money];
    [aVi addSubview:twLab];
    [twLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(140, 20));
        make.left.equalTo(oneLab.mas_right).offset(5);
        make.top.equalTo(aVi.mas_top).offset(5);
        
    }];
    
    
    
    
}


-(void)busRenGouBtnClick
{
    //    创建一个View;
    view=[[UIView alloc]initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, self.view.frame.size.width, 200)];
    view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:view];
    
    UILabel *typeLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 100, 20)];
    typeLab.font = [UIFont systemFontOfSize:12];
    typeLab.text = @"房款状态";
    [view addSubview:typeLab];
    
    //线
    for (int i = 0; i<2; i++)
    {
        UILabel *linLab = [[UILabel alloc] init];
        linLab.frame = CGRectMake(0, 62+63*i, B_WIDTH, 0.5);
        linLab.backgroundColor = [UIColor grayColor];
        [view addSubview:linLab];
    }
    NSArray *titArr= @[@"已交清",@"未交清"];
    int AA = (B_WIDTH-20*2-20)/2;
    for (int m = 0; m <2; m++)
    {
        UIButton *chengBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        chengBtn.frame = CGRectMake(20+AA*m, 35, AA-10, 20);
        chengBtn.layer.cornerRadius = 2;
        chengBtn.layer.borderWidth = 1;
        chengBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [chengBtn setTitleColor:[UIColor blackColor] forState:0];
        chengBtn.layer.borderColor = [UIColor grayColor].CGColor;
        chengBtn.tag = 1000+m;
        [chengBtn addTarget:self action:@selector(gouAddBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [chengBtn setTitle:titArr[m] forState:UIControlStateNormal];
        [view addSubview:chengBtn];
        
    }
    
    UILabel *tuiLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, 100, 20)];
    tuiLab.font = [UIFont systemFontOfSize:12];
    tuiLab.text = @"物业类型";
    [view addSubview:tuiLab];
    
    NSArray *titArr1= @[@"车位",@"储藏室"];
    for (int n = 0; n <2; n++)
    {
        UIButton *tuiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tuiBtn.frame = CGRectMake(20+AA*n, 95, AA-10, 20);
        tuiBtn.layer.cornerRadius = 2;
        tuiBtn.layer.borderWidth = 1;
        tuiBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [tuiBtn setTitleColor:[UIColor blackColor] forState:0];
        tuiBtn.layer.borderColor = [UIColor grayColor].CGColor;
        tuiBtn.tag = 1002+n;
        [tuiBtn addTarget:self action:@selector(gouAddBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [tuiBtn setTitle:titArr1[n] forState:UIControlStateNormal];
        [view addSubview:tuiBtn];
        
    }
    
    
    //确定按钮
    
    UIButton*determineBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 145, B_WIDTH-10*2, 35)];
    determineBtn.layer.cornerRadius = 5;
    [determineBtn setTitle:@"确定" forState:UIControlStateNormal];
    [determineBtn addTarget:self action:@selector(determineBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    determineBtn.backgroundColor=[UIColor orangeColor];
    [view addSubview:determineBtn];
    
}
//四个按钮的点击事件,用来记录点击了什么
-(void)gouAddBtnClick:(UIButton*)btn
{
    UIButton*btn1=[self.view viewWithTag:1000];
    UIButton*btn2=[self.view viewWithTag:1001];
    UIButton*btn3=[self.view viewWithTag:1002];
    UIButton*btn4=[self.view viewWithTag:1003];
    
    if (btn.tag==1000) {
        //点击了一排第一个
        one=@"1";
        btn1.backgroundColor=[UIColor orangeColor];
        btn2.backgroundColor=[UIColor whiteColor];
    }else if (btn.tag==1001)
    { //点击了一排第二个
        one= @"2";
        btn2.backgroundColor=[UIColor orangeColor];
        btn1.backgroundColor=[UIColor whiteColor];
        
    }else if (btn.tag==1002)
    { //点击了二排第一个
        tow=@"2";
        btn3.backgroundColor=[UIColor orangeColor];
        btn4.backgroundColor=[UIColor whiteColor];
    }else if (btn.tag==1003)
    { //点击了二排第二个
        tow=@"4";
        btn4.backgroundColor=[UIColor orangeColor];
        btn3.backgroundColor=[UIColor whiteColor];
    }
    
    
}

//确定按钮的点击事件,用来掉接口
-(void)determineBtnClick:(UIButton *)btn
{
    NSLog(@"走这一步了没有%@---%@",one,tow);
    //点击后直接进行删除view
    [view removeFromSuperview];
    //然后调接口
    
    [_manager userGMPeiRenGouWithID:uid withSouSuo:nil withSouType:@"0" withWuYe:tow withFangK:one];
    NSLog(@"---------1111------one-%@-------tow-%@----",one,tow);
    [_tablView reloadData];
    //最后让两个int归零,方便下次点击继续记录
    one=@"0" ;
    tow=@"0";
}


-(void)peiGouXuanBtnClick:(UIButton *)sender
{
    NSArray *arr = @[@"客户",@"员工",];
    [FTPopOverMenu showForSender:sender
                   withMenuArray:arr
                      imageArray:nil
                       doneBlock:^(NSInteger selectedIndex)//0 . . . .
     {
         [sender setTitle:arr[selectedIndex] forState:UIControlStateNormal];
         
         NSString *yuanStr = arr[selectedIndex];
         
         if ([yuanStr isEqualToString:@"员工"])
         {
             keStr = @"1";
         }
         else
         {
             keStr = @"0";
         }
         
     }
                    dismissBlock:^
     {
         
     }];
    
    
}


#pragma mark- 搜索的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
    if ([IDA isEqualToString:@"1"])
    {
        [_manager userGMPeiRenGouWithID:uid withSouSuo:searchBar.text withSouType:nil withWuYe:nil withFangK:nil];
        

        
        [_tablView reloadData];
    }
    if ([IDA isEqualToString:@"2"])
    {
        
        [_manager userGMPeiRenGouWithID:uid withSouSuo:searchBar.text withSouType:keStr withWuYe:nil withFangK:nil];
        
        
        
        [_tablView reloadData];
    }
    

    [searchBar resignFirstResponder];
    
    
    
}
//当搜索框中的内容发生改变时会自动进行搜索,这个是经常用的

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    if (searchText.length == 0) {
        [_manager userGMPeiRenGouWithID:uid withSouSuo:nil withSouType:@"0" withWuYe:nil withFangK:nil];

        
        [searchBar resignFirstResponder];
        [view endEditing:YES];
        [_tablView reloadData];
    }else {
        
        [_manager userGMPeiRenGouWithID:uid withSouSuo:searchText withSouType:@"0" withWuYe:nil withFangK:nil];

        
        //更新UI
        [_tablView reloadData];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    

       return _busDataArr.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndent = @"Cell";
    PeiTaoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
    if (cell == nil)
    {
        cell = [[PeiTaoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
        
    }
    if ([cell isKindOfClass:[PeiTaoCell class]])
    {
        if (_busDataArr.count != 0)
        {
            NSDictionary *dict = [_busDataArr objectAtIndex:indexPath.row];
            
            [(PeiTaoCell *)cell setUpCellWithName:dict[@"rengou_name"] andHouse:dict[@"room_name"] andData:dict[@"rengou_time"] andZhungTai:dict[@"sf_status"] andPhone:dict[@"rengou_phone"]andGuWen:dict[@"adviser"]];
            
           // NSLog(@"------%@---",dict);
        }
    }
    

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PeiTaoXVC *peiXVC = [[PeiTaoXVC alloc] init];
    peiXVC.imID = [[_busDataArr objectAtIndex:indexPath.row] objectForKey:@"under_id"];
  //  NSLog(@"1111没有ID%@",[[_busDataArr objectAtIndex:indexPath.row] objectForKey:@"under_id"]);
    [self.navigationController pushViewController:peiXVC animated:NO];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
